package com.whfc.base.xxl.job;

import com.whfc.XxlJobConfig;
import com.whfc.base.manager.WeatherMgr;
import com.whfc.fuum.dto.AppProjectDTO;
import com.whfc.fuum.service.AppProjectService;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.stereotype.Component;

import java.util.List;


/**
 * @Description: 定时刷新项目的天气预报
 * @author: sunguodong
 * @version: 1.0
 * @date: 2019/10/24
 */
@Component
@ConditionalOnBean(XxlJobConfig.class)
public class ProjectWeatherJob {
    
    @DubboReference(interfaceClass = AppProjectService.class, version = "1.0.0")
    private AppProjectService appProjectService;

    @Autowired
    private WeatherMgr weatherMgr;

    /**
     * 每天凌晨4点
     */
    @XxlJob("refreshWeather")
    public void refreshWeather() {
        try {
            List<AppProjectDTO> projectDTOList = appProjectService.getProjectList();
            if (projectDTOList != null && projectDTOList.size() > 0) {
                for (AppProjectDTO project : projectDTOList) {
                    if (project.getDeptId() != null && project.getLng() != null && project.getLat() != null) {
                        //刷新预报天气
                        weatherMgr.refreshWeather(project.getDeptId(), project.getLng(), project.getLat());
                    }

                }
            }
        } catch (Exception ex) {
            XxlJobHelper.handleFail("定时刷新项目的天气预报,error" + ex.getMessage());
        }
    }
}
